﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Kitware.VTK;

namespace HAVTKPack
{
   public class ClipGeometryFilter:GeometryFilter
    {
       public ClipGeometryFilter()
           : base("clip")
       {
 
       }

        vtkImplicitPlaneWidget _planeWidget;

        vtkPlane _plane;

        vtkClipPolyData _clipPolyData;

        vtkPolyDataMapper _selectMapper;
        //vtkPolyDataMapper _dataMapper;

        vtkActor _selectActor;
        //vtkActor _dataActor;


       /// <summary>
       /// 渲染是有顺序的
       /// </summary>
       public override void Render()
       {

           //vtkPolyData data = this.ParentNode.TransFormPolyDataFilter.GetOutput();
           //vtkPolyData data = this.ParentNode.Geometry.PolyData;
           vtkPolyData data = this.InputPolyData ;


           data.Update();


           _plane = vtkPlane.New();

           _clipPolyData = vtkClipPolyData.New();
           _clipPolyData.SetInput(data);
           _clipPolyData.SetClipFunction(_plane);
           _clipPolyData.InsideOutOn();



           //this.OutputPolyData = this._clipPolyData.GetOutput();
           this.OutputPolyData = InputPolyData;

           _selectMapper = vtkPolyDataMapper.New();
           _selectMapper.SetInputConnection(_clipPolyData.GetOutputPort());
           _selectActor = vtkActor.New();
           _selectActor.SetMapper(_selectMapper);
           _selectActor.VisibilityOff();
           _selectActor.SetScale(1.01, 1.01, 1.01);

           this.ParentNode.Scene.VtkControl.RenderWindow.GetRenderers().GetFirstRenderer().AddActor(_selectActor);

           //this.ParentNode.Scene.VtkControl.RenderWindow.GetRenderers().GetFirstRenderer().AddActor(_dataActor);

          

           _planeWidget = vtkImplicitPlaneWidget.New();

           _planeWidget.SetInteractor(this.ParentNode.Scene.VtkControl.RenderWindow.GetInteractor());

           _planeWidget.SetPlaceFactor(1.25);

           _planeWidget.SetInput(data);

           //_planeWidget.SetOrigin(0.275476411022962, -0.28454308859152, 0.18674604754046);

           _planeWidget.SetNormalToYAxis(1);

           _planeWidget.PlaceWidget();

           _planeWidget.OutlineTranslationOff();
          

           _planeWidget.On();

           

           _planeWidget.InteractionEvt += new vtkObject.vtkObjectEventHandler(_planeWidget_InteractionEvt);

       }

       public override void StopRender()
       {


           //throw new NotImplementedException();
       }


       void _planeWidget_InteractionEvt(vtkObject sender, vtkObjectEventArgs e)
       {

           _planeWidget.GetPlane(this._plane);

           _selectActor.VisibilityOn();

       }

    }
}
